package com.wf.smartcomm.server.daoImpl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.wf.smartcomm.server.bean.Scores;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.dao.ScoreDao;
import com.wf.smartcomm.server.model.Page;
import com.wf.smartcomm.server.util.PageUtils;

@Repository
public class ScoreDaoImpl implements ScoreDao {

	@Resource
	private SessionFactory sessionFactory;

	@Override
	public Page list(User user, Page page) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("select count(*) from Scores where customer =:customer");
		query.setParameter("customer", user);
		Long counts = (Long) query.uniqueResult();
		page = PageUtils.createPage(page, counts.intValue());
		query = session.createQuery("from Scores where customer =:customer order by createdAt desc");
		query.setParameter("customer", user);
		query.setFirstResult(page.getBeginIndex());
		query.setMaxResults(page.getPageSize());
		page.setDataList(query.list());
		session.flush();
		return page;
		
	}
	
	/*@Override
	public List<Scores> getScoreByUser(User user) {
		Query query= sessionFactory.getCurrentSession().createQuery("from Scores where customer=:customer order by createdAt desc");
		query.setParameter("customer", user);
		return null;
	}*/

}
